<?php
	session_start();
	
	require_once('../../lib/'.'BaseDir.inc.htm');
	require_once (EIU_LIB.'View.class.htm');
	require_once(EIU_LIB.'DBCommon.class.htm');
	
	if(!empty($_POST['cusId'])){
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		
		//Load Customer List
		$q = "
			SELECT A.CusID,A.ComName,A.ShortName,A.ComAddress,A.ComTel,A.ComFax,A.ParentID,A.BankName,A.BankCN,A.BankAcc,A.isDeleted,A.CreateDate,A.LastUpdate,A.ModifiedBy,B.ComName  AS ParentName
			FROM cus_list_tbl AS A LEFT JOIN cus_list_tbl AS B ON A.ParentID = B.CusID 
			WHERE A.isDeleted = 0
		";
				
		$rs = $oDB->stl_db_select($q, $conn);
		//print_r($rs);
		if(is_array($rs)){
			foreach($rs as $key=>$value){
				if($rs[$key]->isDeleted=='0'){											
					
					$data1['cuslist'][$key]['cusname'] = $rs[$key]->ComName;					
					$data1['cuslist'][$key]['cusid'] = $rs[$key]->CusID;					
					$data1['cuslist'][$key]['custel'] = $rs[$key]->ComTel;					
					$data1['cuslist'][$key]['cusadr'] = $rs[$key]->ComAddress;	
					
					if($rs[$key]->CusID == $_POST['cusId'])
					{
						$data1['cusId'] = $rs[$key]->CusID;
						$data1['name'] = $rs[$key]->ComName;
						$data1['shortname'] = $rs[$key]->ShortName;
						$data1['address'] = $rs[$key]->ComAddress;
						$data1['tel'] = $rs[$key]->ComTel;
						$data1['fax'] = $rs[$key]->ComFax;
						
						$data1['bankname'] = $rs[$key]->BankName;
						$data1['bankcn'] = $rs[$key]->BankCN;
						$data1['bankacc'] = $rs[$key]->BankAcc;
						$data1['parentid'] = $rs[$key]->ParentID;
						$data1['parentname'] = $rs[$key]->ParentName;
					}
				}
			}	
			
		}		
		
		echo json_encode($data1);
		$oDB->stl_closeConn($conn);
		
	}else{
	
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		//Load Customer List
		$q = "SELECT CusID,ComName,ShortName,ComAddress,ComTel,ComFax,ParentID,BankName,BankCN,BankAcc,isDeleted,CreateDate,LastUpdate,ModifiedBy FROM cus_list_tbl WHERE isDeleted = 0";
				
		$rs = $oDB->stl_db_select($q, $conn);
		
		if(is_array($rs)){
			foreach($rs as $key=>$value){
				if($rs[$key]->isDeleted=='0'){					
					$data1[$key]['cusname'] = $rs[$key]->ComName;					
					$data1[$key]['cusid'] = $rs[$key]->CusID;					
					$data1[$key]['custel'] = $rs[$key]->ComTel;					
					$data1[$key]['cusadr'] = $rs[$key]->ComAddress;			
				}
			}
		}
		echo json_encode($data1);
		$oDB->stl_closeConn($conn);
	}
?>
